@extends('layouts.application')
@section('css')
    <style>
        .daterangepicker {
            display: none;
        }

        .am-btn {
            padding-bottom: 7px;
            padding-top: 7px;
        }

        .am-form-field {
            margin-left: 2px;
        }
    </style>

    <link rel="stylesheet" href="/vendor/daterangepicker/daterangepicker.css">
    {{--<link rel="stylesheet" href="/js/bootstrap.min.css">--}}
@endsection
@section('content')


    <!-- content start -->
    <div class="admin-content">
        <div class="admin-content-body">
            <div class="am-cf am-padding am-padding-bottom-0">
                <div class="am-fl am-cf"><strong class="am-text-primary am-text-lg">商品</strong> /
                    <small>Product List</small>
                </div>
            </div>
            @include('layouts.shared._flash')
            <hr>

            <div class="am-g">
                <div class="am-u-sm-12 am-u-md-6">
                    <div class="am-btn-toolbar">
                        <div class="am-btn-group am-btn-group-xs">
                            <a href="{{route('shop.product.create')}}" type="button" class="am-btn am-btn-default"><span
                                        class="am-icon-plus"></span> 新增
                            </a>
                            <button type="button" id="recycle_all" class="am-btn am-btn-default"><span
                                        class="am-icon-trash-o"></span> 回收
                            </button>
                        </div>
                    </div>
                </div>

            </div>
            <hr>


            <div class="am-g">

                <form class="am-form-inline" role="form" method="get">

                    <div class="am-form-group">
                        <input style="width: 130px;height: 32px;margin: 2px 2px" type="text" class="am-form-field"
                               name="name"
                               placeholder="按商品" value="{{Request::input('name')}}">

                    </div>

                    <div class="am-form-group">
                        <select multiple data-am-selected="{btnSize: 'sm', maxHeight: 360, searchBox: 1}"
                                name="category_id[]">

                            <optgroup>
                                <option value="-1">所有分类</option>
                            </optgroup>
                            @foreach($filter_categories as $category)
                                <optgroup label="{{$category->name}}">
                                    @foreach($category->children as $child)
                                        <option value="{{$child->id}}"
                                        @if($child->id == Request::input('category_id')) selected @endif>{{$child->name}}</option>
                                    @endforeach
                                </optgroup>
                            @endforeach
                        </select>
                    </div>

                    <div class="am-form-group">
                        <select data-am-selected="{btnSize: 'sm', maxHeight: 360, searchBox: 1}"
                                name="brand_id">
                                <option value="-1">所有品牌</option>
                            @foreach($brands as $brand)
                                <option value="{{$brand->id}}"
                                @if($brand->id == Request::input('brand_id')) selected @endif>{{$brand->name}}</option>
                            @endforeach
                        </select>
                    </div>

                    <div class="am-form-group am-input-group-sm">
                        <div class="am-btn-group" data-am-button>
                            <label class="am-btn am-btn-default am-btn-sm">
                                <input type="checkbox" name="is_top"> 置顶
                            </label>
                            <label class="am-btn am-btn-default am-btn-sm">
                                <input type="checkbox" name="is_recommend"> 推荐
                            </label>
                            <label class="am-btn am-btn-default am-btn-sm">
                                <input type="checkbox" name="is_hot"> 热销
                            </label>
                            <label class="am-btn am-btn-default am-btn-sm">
                                <input type="checkbox" name="is_new"> 新品
                            </label>
                        </div>
                    </div>

                    {{--<div class="am-btn-group" data-am-button>--}}
                        {{--<label class="am-btn am-btn-default am-btn-xs">--}}
                        {{--<input type="checkbox" name="is_top" > 置顶--}}
                        {{--</label>--}}
                        {{--<label class="am-btn am-btn-default am-btn-xs am-radius @if(Request::input('is_top') == 1) am-active @endif">--}}
                            {{--<input type="checkbox" name="is_top"  @if(Request::input('is_top') == 1) checked @endif>--}}
                            {{--置顶--}}
                        {{--</label>--}}
                        {{--<label class="am-btn am-btn-default am-btn-xs">--}}
                            {{--<input type="checkbox" name="is_recommend"--}}
                            {{--@if(Request::input('is_recommend') == 1) checked @endif> 推荐--}}
                        {{--</label>--}}
                        {{--<label class="am-btn am-btn-default am-btn-xs">--}}
                            {{--<input type="checkbox" name="is_new"--}}
                                   {{--@if(Request::input('is_new') == 1) checked @endif> 新品--}}
                        {{--</label>--}}
                        {{--<label class="am-btn am-btn-default am-btn-xs">--}}
                            {{--<input type="checkbox" name="is_hot"--}}
                            {{--@if(Request::input('is_hot') == 1) checked @endif> 热销--}}
                        {{--</label>--}}

                    {{--</div>--}}

                    <div class="am-form-group">
                        <select data-am-selected="{btnSize: 'sm', maxHeight: 360, searchBox: 1}"
                        name="is_onsale">
                            <option value="-1"
                            @if(Request::input('is_onsale') == -1) selected @endif>上架状态</option>
                            <option value="1"
                            @if(Request::input('is_onsale')== 1) selected @endif>已上架</option>
                            <option value="0"
                            >未上架</option>

                        </select>
                    </div>

                    <div class="am-form-group">
                        <div class="am-input-group am-input-group-sm">
                            <input type="text" id="created_at" name="created_at" class="am-form-field" style="width: 180px">
                            <span class="am-input-group-btn">
                            <button class="am-btn am-btn-default" type="submit">搜索</button>
                            </span>
                            <span class="am-input-group-btn">
                            <a href="{{route('shop.product.index')}}" class="am-btn am-btn-default" type="button">重置</a>
                            </span>
                        </div>
                    </div>
                </form>
            </div>


            <div class="am-g">
                <div class="am-u-sm-12">
                    <form class="am-form">
                        <table class="am-table am-table-striped am-table-hover table-main">
                            <thead>
                            <tr>
                                <th class="table-check"><input type="checkbox" class="check_all"></th>
                                <th class="table-id">ID</th>
                                <th class="table-id">缩略图</th>
                                <th class="table-title">所属分类</th>
                                <th class="table-title">商品名称</th>
                                <th class="table-title">品牌</th>
                                <th class="table-type">单价/元</th>
                                <th class="table-type">上架</th>
                                <th class="table-type">置顶</th>
                                <th class="table-type">推荐</th>
                                <th class="table-type">新品</th>
                                <th class="table-type">热销</th>
                                <th class="table-type">库存</th>
                                <th class="table-date am-hide-sm-only">上架日期</th>
                                <th class="table-set">操作</th>
                            </tr>
                            </thead>
                            <tbody>
                            @foreach($products as $product)
                                <tr data-id="{{$product->id}}">
                                    <td><input type="checkbox" value="{{$product->id}}" class="checked_id"
                                               name="checked_id[]"></td>
                                    <td>{{$product->id}}</td>
                                    <td>
                                        <img src="{{$product->thumb}}" alt="" style="width: 50px;height: 50px">

                                    </td>

                                    <td>{{$product->categories->implode('name', ' ,')}}</td>
                                    <td>{{$product->name}}</td>
                                    <td class="am-hide-sm-only">{{$product->brand->name or ''}}</td>
                                    <td class="am-hide-sm-only">
                                        <input type="text" value="{{$product->price}}" style="width:70px" class="price">

                                    </td>
                                    {{--<td class="am-hide-sm-only">--}}
                                        {{--{!! is_something($product,'is_onsale') !!}--}}

                                    {{--</td>--}}

                                    @foreach($infos as $info)
                                        <td data-attr="{{$info}}" class="table-type">
                                            {!! is_something($product,$info) !!}
                                        </td>
                                    @endforeach
                                    <td class="am-hide-sm-only">
                                        <input type="text" value="{{$product->stock}}" style="width:50px" class="stock">
                                    </td>
                                    <td class="am-hide-sm-only">{{$product->created_at}}</td>
                                    <td>
                                        <div class="am-btn-toolbar">
                                            <div class="am-btn-group am-btn-group-xs">
                                                <a href="{{route('shop.product.edit',$product->id)}}"
                                                   class="am-btn am-btn-default am-btn-xs am-text-secondary"><span
                                                            class="am-icon-pencil-square-o"></span> 编辑
                                                </a>
                                                <a href="#"
                                                   class=" recycle_one am-btn am-btn-default am-btn-xs am-text-danger am-hide-sm-only">
                                                    <span class="am-icon-trash-o"></span> 回收
                                                </a>
                                            </div>
                                        </div>
                                    </td>
                                </tr>
                            @endforeach

                            </tbody>
                        </table>
                        <div class="am-cf">
                            共 {{$products->total()}} 条记录
                            <div class="am-fr">
                                <ul class="am-pagination">
                                    {{$products->links()}}
                                </ul>
                            </div>
                        </div>
                        <hr/>
                        <p>注：.....</p>
                    </form>
                </div>

            </div>
        </div>

        <footer class="admin-content-footer">
            <hr>
            <p class="am-padding-left">© 2014 AllMobilize, Inc. Licensed under MIT license.</p>
        </footer>

    </div>
    <!-- content end -->
@endsection
@section('js')
    <script src="/vendor/daterangepicker/moment.js"></script>
    <script src="/vendor/moment/locale/zh-cn.js"></script>
    <script src="/vendor/daterangepicker/daterangepicker.js"></script>
    <script src="/js/daterange_config.js"></script>
    <script>
        $(function () {
            //改变属性
            $('.change_attr').click(function () {
                var id = $(this).parents('tr').data('id');
                var attr = $(this).attr('data-attr');
                var _this = $(this);
                $.ajax({
                    type: 'PATCH',
                    url: "{{route('shop.product.change_attr')}}",
                    data: {id: id, attr: attr},
                    success: function () {
                        _this.toggleClass('am-icon-check am-icon-close')
                    }
                });
                return false;
            })
            //库存
            $('.stock').change(function () {
                var id = $(this).parents('tr').data('id');
                var stock = $(this).val();
                $.ajax({
                    type: 'PATCH',
                    url: "{{route('shop.product.stock')}}",
                    data: {id: id, stock: stock},
                    success: function () {
                    }
                });
                return false;
            })
            //单价
            $('.price').change(function () {
                $.ajax({
                    type: 'PATCH',
                    url: "{{route('shop.product.price')}}",
                    data: {id: $(this).parents('tr').data('id'), price: $(this).val()},
                    success: function () {

                    }
                })
                return false;
            })
            //单个回收
            $('.recycle_one').click(function () {
                var id = $(this).parents('tr').data('id');

                var _this = $(this);
                $.ajax({
                    type: 'PATCH',
                    url: "{{route('shop.product.recycle_one')}}",
                    data: {id: id},
                    success: function () {
                        _this.parents('tr').fadeOut(400);
                    }
                });
                return false;
            })
            //全选，反选
            $('.check_all').click(function () {
                $(':checkbox').prop('checked', this.checked);
            })
            //多选回收
            $('#recycle_all').click(function () {
                var length = $('.checked_id:checked').length;
                if (length == 0) {
                    alert('您至少选中一条！')
                }
                var checked_id = $('.checked_id:checked').serialize()

                $.ajax({
                    type: 'PATCH',
                    url: "{{route('shop.product.recycle_all')}}",
                    data: checked_id,
                    success: function () {
                        $('.checked_id:checked').each(function () {
                            $(this).parents('tr').fadeOut(400);
                        })
                    }
                });
                return false
            })

        })
    </script>
@endsection


